﻿$(document).ready(function () {

    function onPageSelectChanged() {

        pageMetaId = $("#SelectedPageId").val();

        // Do something with the previous value after the change
        $.post(pathPrefix + "Admin/PageMetaByPageMetaId", { "pageMetaId": pageMetaId }, function (result) {

            // Set Page Title
            title = result.Title
            tti = $("#titleTextInput");
            tti.val(title);
            tti.removeAttr("disabled");

            // Set Keywords
            keys = result.Keywords;
            kta = $("#keywordsTextArea");
            kta.val(keys);
            kta.removeAttr("disabled");


            // Set Description
            desc = result.Description;
            dta = $("#descriptionTextArea");
            dta.val(desc);
            dta.removeAttr("disabled");
        });

    }

    $("#SelectedPageId").change(onPageSelectChanged);

    onPageSelectChanged();

    // Posting back
    $("#updateBtn").click(function () {

        pageMetaId = $("#SelectedPageId").val();
        newt = $("#titleTextInput").val();
        newk = $("#keywordsTextArea").val();
        newd = $("#descriptionTextArea").val();

        btn = $(this);
        btn.attr("disabled", "disabled").addClass("disabled");
        
        $.post(pathPrefix + "Admin/UpdatePageMeta", { "pageMetaId": pageMetaId, "title": newt, "keywords": newk, "description": newd }, function (result) {
            btn.removeAttr("disabled").removeClass("disabled");
        });

    });
});